using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;

namespace Yuanlin.Business
{
    public class AquacultureSection_AddPost : YuanlinUserControl
    {
        protected FileUpload pictureAttachment1;
        protected FileUpload pictureAttachment2;
        protected FileUpload pictureAttachment3;
        protected ValuedTextBox descriptionValuedTextBox;
        protected ResourceButton saveButton;
        protected ResourceButton publishedButton;
        protected HiddenField targetControlIDHidden;
        protected HiddenField dataWrapperHidden;
        private string redirectString = "window.location='" + SiteUrls.Instance().Login + "';";
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;
        private int entityId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.AquacultureSectionThreadId);
        private int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);
        private int categoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryId);

        public string DataWrapperID
        {
            get
            {
                string s = ViewState["DataWrapperID"] as string;
                if (s == null)
                {
                    s = string.Empty;
                }
                return s;
            }
            set
            {
                ViewState["DataWrapperID"] = value;
            }
        }
        public string TargetControlID
        {
            get
            {
                string s = ViewState["TargetControlID"] as string;
                if (s == null)
                {
                    s = string.Empty;
                }
                return s;
            }
            set
            {
                ViewState["TargetControlID"] = value;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            if (user.IsAnonymous)
            {
                publishedButton.Visible = true;
                saveButton.Visible = false;
                return;
            }
            saveButton.Click += new EventHandler(SavePost);
        }
        private void SavePost(object sender, EventArgs e)
        {
            AquacultureSection_Thread thread = BusinessManager.GetAquacultureSection_Thread(entityId);

            if (thread != null)
            {
                AquacultureSection_Post post = new AquacultureSection_Post();
                post.AquacultureId.Value = thread.AquacultureId.Value;
                post.SectionId.Value = thread.SectionId.Value;
                post.ThreadId.Value = thread.EntityId.Value;
                post.Body.Value =  CensorWordManager.CensorWord(descriptionValuedTextBox.Value);
                post.AuthorId.Value = user.EntityId.Value;
                post.Author.Value = user.NickName.Value;
                post.CreateDate.Value = DateTime.Now;
                post.CategoryId.Value = categoryId;
                post.CategoryType.Value = categoryType;
                BusinessManager.CreateAquacultureSection_Post(post);

                thread.TotalPosts.Value++;
                thread.MostRecentReplierId.Value = user.EntityId.Value;
                thread.MostRecentReplierName.Value = user.NickName.Value;
                thread.UpdateDate.Value = DateTime.Now;
                BusinessManager.UpdateAquacultureSection_Thread(thread);

                CreatePictures(post.AquacultureId.Value, post.SectionId.Value, post.ThreadId.Value, post.EntityId.Value);

                Page.Response.Redirect(SiteUrls.Instance().GetAquacultureSection_threadUrl(entityId,categoryType,categoryId));

            }
        }
        private void CreatePictures(int aquacultureId, int sectionId, int threadId, int postId)
        {
            CreatePostPicture(aquacultureId, sectionId, threadId, postId, pictureAttachment1);
            CreatePostPicture(aquacultureId, sectionId, threadId, postId, pictureAttachment2);
            CreatePostPicture(aquacultureId, sectionId, threadId, postId, pictureAttachment3);
        }
        private void CreatePostPicture(int aquacultureId, int sectionId, int threadId, int postId, FileUpload pictureAttachment)
        {
            if (SiteUrls.Instance().IsAttachmentValid(pictureAttachment))
            {
                AquacultureSection_PostPicture postPicture = new AquacultureSection_PostPicture();
                postPicture.AttachmentFileName.Value = SiteUrls.Instance().GetPostedFileName(pictureAttachment);
                postPicture.AquacultureId.Value = aquacultureId;
                postPicture.SectionId.Value = sectionId;
                postPicture.ThreadId.Value = threadId;
                postPicture.PostId.Value = postId;
                postPicture.CategoryId.Value = categoryId;
                postPicture.CategoryType.Value = categoryType;
                BusinessManager.CreateAquacultureSection_PostPicture(postPicture);
                SiteUrls.Instance().SaveFile(SiteUrls.Instance().GetFile(postPicture), pictureAttachment);
            }
        }
    }
}